---
title: "Bats and human blood project: median cell sizes: plotting"
output:
  html_document:
    df_print: paged
  pdf_document: default
---

# prepare
```{r}
pathWD.new <- "~/Documents/08_Pojekte/01_dRT-DC/220301_Bob_DataAnalysis_BatsHumans/20230925_SurfaceAreaToVolumeRatio"
setwd(pathWD.new)

pacman::p_load(tidyverse, lmerTest, car, easystats, readr)

```


# load data table
```{r}
surfArea2vol.stats <- read_delim(file = "tbl_surfArea2volRatio_stats.csv",
                                 delim = ",",
                                 col_names = TRUE)

surfArea2vol.stats$Species <- factor(surfArea2vol.stats$Species)
surfArea2vol.stats$Species <- relevel(surfArea2vol.stats$Species, ref = "RousettusAegyptiacus")
surfArea2vol.stats$Species <- relevel(surfArea2vol.stats$Species, ref = "Human")
surfArea2vol.stats$TempLevel <- factor(surfArea2vol.stats$TempLevel)

levels.Species <- levels(surfArea2vol.stats$Species)
levels.Temp <- levels(surfArea2vol.stats$TempLevel)

surfArea2vol.stats.allTemp <- subset(surfArea2vol.stats, TempLevel == "All")
```

# plot prepare
```{r}
labels.Species <- c("Homo\nsapiens", "Rousettus\naegyptiacus", "Nyctalus\nnoctula")
names(labels.Species) <- levels.Species

labels.Species.Short <- c("Homo\nsap", "Rou\naeg", "Nyc\nnoc")
names(labels.Species.Short) <- levels.Species

labels.Species.OneLine <- c(expression(italic("Homo sapiens")), expression(italic("Rousettus aegyptiacus")), expression(italic("Nyctalus noctula")))
names(labels.Species.OneLine) <- levels.Species

# axes titles
title.Species <- "Species"
# title.surfArea2vol <- "Surface area / volume (µm^-1)"
title.surfArea2vol <- expression("Surface area / volume (µm"^-1*")")
title.vol2surfArea <- "Volume / surface area (µm)"

# color scheme
colorPalette.Species <- c("#C21618","#F3BA29","#1F77BD")
```


## all plots surface area / volume
```{r fig.height=10/2.54, fig.width=12/2.54}
plots.surfArea2vol.Species.limits <- data.frame(min = c(1),
                                                max = c(2))

plots.surfArea2vol.Species <- ggplot(data = surfArea2vol.stats.allTemp)

# define layout parameters
plots.surfArea2vol.Species <- plots.surfArea2vol.Species +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15, margin = margin(t=10)),
        axis.text.x = element_text(size = 10, face = "bold", margin = margin(t=5)),
        axis.title.y = element_text(size = 15, margin = margin(r=10)),
        axis.text.y = element_text(size = 10, face = "bold"),
        panel.border = element_rect(size = 1.5, colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(size = 20, face = "bold", hjust = 0.5, vjust = 1.0),
        plot.subtitle = element_text(size = 15, hjust = 0.5, vjust = 0),
        plot.caption = element_text(size = 8, face = "italic", vjust = 0, margin = margin(t=10)),
        legend.position = "none")

# plot mean
plots.surfArea2vol.Species <- plots.surfArea2vol.Species +
  geom_point(mapping = aes(x = Species,
                           y = SurfArea2Vol_mean,
                           colour = Species),
             stat = "identity", # important since we provide mean values already
             size = 3)

# plot error bars
plots.surfArea2vol.Species <- plots.surfArea2vol.Species +
  geom_errorbar(mapping = aes(x = Species,
                              ymin = SurfArea2Vol_mean - SurfArea2Vol_sem,
                              ymax = SurfArea2Vol_mean + SurfArea2Vol_sem,
                              colour = Species),
                size = 1.0,
                width = 0.15,
                position = position_dodge())

# add discrete x-axis labels
plots.surfArea2vol.Species <- plots.surfArea2vol.Species +
  scale_x_discrete(labels = labels.Species)

# add further layout parameter
plots.surfArea2vol.Species <- plots.surfArea2vol.Species +
  scale_y_continuous(limits = c(0, plots.surfArea2vol.Species.limits$max),
                     expand = expansion(mult = c(0.025, 0.025))) +
  labs(x = title.Species,
       y = title.surfArea2vol) +
  scale_color_manual(values = colorPalette.Species,
                     breaks = names(labels.Species),
                     labels = labels.Species)

# change coordinates = "zoom in"
plots.surfArea2vol.Species <- plots.surfArea2vol.Species +
  coord_cartesian(ylim = c(plots.surfArea2vol.Species.limits$min, plots.surfArea2vol.Species.limits$max))

# save plots
ggsave(filename = file.path(pathWD.new, paste0("surfArea2vol", "_Species", ".svg")),
       width = 12, height = 10, units = "cm", dpi = 600, device = "svg",
       plot = plots.surfArea2vol.Species, scale = 1)

ggsave(filename = file.path(pathWD.new, paste0("surfArea2vol", "_Species", ".png")),
       width = 12, height = 10, units = "cm", dpi = 600, device = "png",
       plot = plots.surfArea2vol.Species, scale = 1)

plots.surfArea2vol.Species

```



## all plots volume / surface area
```{r fig.height=10/2.54, fig.width=12/2.54}
plots.vol2surfArea.Species.limits <- data.frame(min = c(.5),
                                                max = c(.9))

plots.vol2surfArea.Species <- ggplot(data = surfArea2vol.stats.allTemp)

# define layout parameters
plots.vol2surfArea.Species <- plots.vol2surfArea.Species +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15, margin = margin(t=10)),
        axis.text.x = element_text(size = 10, face = "bold", margin = margin(t=5)),
        axis.title.y = element_text(size = 15, margin = margin(r=10)),
        axis.text.y = element_text(size = 10, face = "bold"),
        panel.border = element_rect(size = 1.5, colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.title = element_text(size = 20, face = "bold", hjust = 0.5, vjust = 1.0),
        plot.subtitle = element_text(size = 15, hjust = 0.5, vjust = 0),
        plot.caption = element_text(size = 8, face = "italic", vjust = 0, margin = margin(t=10)),
        legend.position = "none")

# plot mean
plots.vol2surfArea.Species <- plots.vol2surfArea.Species +
  geom_point(mapping = aes(x = Species,
                           y = Vol2SurfArea_mean,
                           colour = Species),
             stat = "identity", # important since we provide mean values already
             size = 3)

# plot error bars
plots.vol2surfArea.Species <- plots.vol2surfArea.Species +
  geom_errorbar(mapping = aes(x = Species,
                              ymin = Vol2SurfArea_mean - Vol2SurfArea_sem,
                              ymax = Vol2SurfArea_mean + Vol2SurfArea_sem,
                              colour = Species),
                size = 1.0,
                width = 0.15,
                position = position_dodge())

# add discrete x-axis labels
plots.vol2surfArea.Species <- plots.vol2surfArea.Species +
  scale_x_discrete(labels = labels.Species)

# add further layout parameter
plots.vol2surfArea.Species <- plots.vol2surfArea.Species +
  scale_y_continuous(limits = c(0, plots.vol2surfArea.Species.limits$max),
                     expand = expansion(mult = c(0.025, 0.025))) +
  labs(x = title.Species,
       y = title.vol2surfArea) +
  scale_color_manual(values = colorPalette.Species,
                     breaks = names(labels.Species),
                     labels = labels.Species)

# change coordinates = "zoom in"
plots.vol2surfArea.Species <- plots.vol2surfArea.Species +
  coord_cartesian(ylim = c(plots.vol2surfArea.Species.limits$min, plots.vol2surfArea.Species.limits$max))

# save plots
ggsave(filename = file.path(pathWD.new, paste0("vol2surfArea", "_Species", ".svg")),
       width = 12, height = 10, units = "cm", dpi = 600, device = "svg",
       plot = plots.vol2surfArea.Species, scale = 1)

ggsave(filename = file.path(pathWD.new, paste0("vol2surfArea", "_Species", ".png")),
       width = 12, height = 10, units = "cm", dpi = 600, device = "png",
       plot = plots.vol2surfArea.Species, scale = 1)

plots.vol2surfArea.Species

```

